<template>
  <div>
    <h1>表格</h1>

    <el-form :inline="true" :model="search" class="demo-form-inline">
      <el-form-item label="用户名">
        <el-input v-model="search.name" placeholder="用户名"></el-input>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" @click="removesel">选中删除</el-button>
        <el-button type="primary" @click="searchs">查询</el-button>
      </el-form-item>
    </el-form>

    <el-table
      ref="multipleTable"
      :data="reslist"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="sel"
    >
      <el-table-column type="selection" width="55"> </el-table-column>

      <!-- 选中删除的一条数据 -->
      <el-table-column prop="uid" lable="id" width="120"> </el-table-column>
      
      <el-table-column label="头像" width="120">
        <template slot-scope="scope">
          <el-image
            style="width: 50px; height: 50px"
            :src="scope.row.avatar"
            :preview-src-list="imglist"
            @click="setsrc(scope.row.avatar)"
          >
          </el-image>
          <!-- <img :src="scope.row.avatar" alt="" /> -->
        </template>
      </el-table-column>

      <el-table-column prop="nickname" label="姓名" width="120">
      </el-table-column>

      <el-table-column prop="asress" label="地址" show-overflow-tooltip>
      </el-table-column>

      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="remove">删除</el-button>
          <el-button @click="editFun(scope.row)">编辑</el-button>
        </template>
      </el-table-column>

    </el-table>


    
    <!-- 分页器 -->
    <el-pagination
      :current-page="pageNum"
      :page-size="pageSize"
      layout="prev, pager, next"
      :total="list.length"
      @current-change="change"
    >
    </el-pagination>

    <el-dialog title="编辑信息" :visible.sync="showEdit">
      <el-form :model="edit">
        <el-form-item label="用户名">
          <el-input v-model="edit.nickname"></el-input>
        </el-form-item>
        <el-form-item label="地址">
          <el-input v-model="edit.asress"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="showEdit = false">取 消</el-button>
        <el-button type="primary" @click="showEdit = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data() {
    return {
      list: [],
      copylist: [],
      search: {
        name: "",
      },
      pageNum: 1,
      pageSize: 5,
      edit: {
        nickname: "",
        adress: "",
        uid: "",
      },
      showEdit: false,
      removeIds: "",
      imglist: [],
    };
  },
  computed: {
    reslist() {
      return this.list.slice(
        this.pageSize * (this.pageNum - 1),
        this.pageSize * this.pageNum
      );
    },
  },
  methods: {
    searchs() {
      //搜索步骤
      this.list = this.copylist.filter((item) => {
        return item.nickname.includes(this.search.name);
      });
    },
    change(val) {
      this.pageNum = val;
    },
    editFun(item) {
      this.showEdit = true;
      this.edit = item;
    },

    // 删除一条信息
    remove(index) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.list.splice(index, 1);
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    // 选中删除
    sel(val) {
      console.log(val);
      let res = val.map((item) => {
        return item.uid; 
      });
      console.log(res);
      // 把要删除的id存起来  存成一个字符串
      this.removeIds = res.join(",");
    },
    removesel() {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 把不善的也就是在要删除的id中不存在的留下来
          this.list = this.list.filter((item) => {
            if (this.removeIds.indexOf(item.uid) == -1) {
              return item;
            }
          });
          this.$message({
            type: "success",
            message: "删除成功!",
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    setsrc(src) {
      this.imglist.push(src);
    },
  },
  mounted() {
    this.$axios.get("/list.json").then((res) => {
      this.list = res.data;
      this.copylist = res.data;
    });
  },
};
</script>
<style lang="scss">
img {
  width: 40px;
  height: 40px;
}
</style>
